//
//  netRequestMethod.m
//  YiChatProject
//
//  Created by mac on 16/7/7.
//  Copyright © 2016年 Chenjun. All rights reserved.
//

#import "netRequestMethod.h"

static MBProgressHUD *hud = nil;
@implementation netRequestMethod
+ (void)sendRequestMethod:(NSString *)postOrGet withRequestURL:(NSString *)url withHttpBodyDic:(NSDictionary *)httpBodyDic withRequestBlock:(requestBlock)block
{
        [self showHUD];
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        manager.requestSerializer = [AFHTTPRequestSerializer serializer];
        manager.responseSerializer = [AFHTTPResponseSerializer serializer];
        if ([postOrGet isEqualToString:@"POST"]) {
            [manager POST:url parameters:httpBodyDic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                [self hideHUD];
                block(responseObject);
            } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                [self hideHUD];
               
                NSLog(@"%@",error);
            }];
        }
        if ([postOrGet isEqualToString:@"GET"]) {
            [manager GET:url parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                [self hideHUD];
                block(responseObject);
            } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                [self hideHUD];
               
                NSLog(@"%@",error);
            }];
        }
}
+ (void)showHUD
{
    if (!hud) {
        hud = [MBProgressHUD showHUDAddedTo:WINDOW animated:YES];
    }
    [hud show:YES];
}
+ (void)hideHUD
{
    if (hud) {
        [hud hide:YES];
    }
}
- (void)startMonitoring
{
    AFNetworkReachabilityManager *manger = [AFNetworkReachabilityManager manager];
    [manger startMonitoring];
    [manger setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        switch (status) {
            case AFNetworkReachabilityStatusUnknown:
            {
                NSLog(@"未知网络");
            }
                break;
            case AFNetworkReachabilityStatusNotReachable:
            {
                NSLog(@"无法连接当前网络");
            }
                break;
            case AFNetworkReachabilityStatusReachableViaWWAN:
            {
                NSLog(@"正在使用移动网络");
            }
                break;
            case AFNetworkReachabilityStatusReachableViaWiFi:
            {
                NSLog(@"正在连接WiFi使用");
            }
                break;
            default:
                break;
        }
    }];
}
+ (BOOL)checkNetIsOrWork
{
    AFNetworkReachabilityManager * manger = [AFNetworkReachabilityManager manager];
    return manger.reachable;
}
@end
